<template>
	<view class="mine-content">
		<image class="mine-content__bjImg" mode="widthFix" src="../../static/img/me_bg@3x.png"></image>
		<view class="mine-body">
			<view class="mine-photoBox">
				<!--  -->
				
				<image class="mine-photoBox__img" mode="aspectFill" :src="userObj.avatar | tacitlyApprove('avatar')"></image>
			</view>
			<p class="mine-name">{{userObj.nickname | tacitlyApprove('nickname')}}</p>

			<view class="mine-pointsBox">
				<button :open-type="item.title == '联系客服' ? 'contact' : item.title == '授权微信个人信息' ? '' : ''" class="mine-pointsBoxItem" v-for="(item,index) in points" :key="index" @click="allanAbrecht(item.title)">
					<span class="mine-pointsBoxItem__title">{{item.title}}</span>
				    <image class="mine-pointsBoxItem__img" mode="widthFix" src="../../static/img/icon_right@3x.png"></image>
				</button>
			</view>
		</view>
	</view>
</template>

<script>
	import "@/static/scss/mine.scss"
	export default {
		data() {
			return {
				points: [{
						title: '授权微信个人信息'
					},
					{
						title: '联系客服'
					},
				]
			}
		},
		filters:{
			tacitlyApprove(val,type){
				if(type == "avatar"){
					 if(!val){
						 return "../../static/img/user_tx.jpg";
					 }
					 return val;
				}else if(type == "nickname"){
					if(!val){
						return "当事君";
					}
					return val;
				}
			}
		},
		computed:{
			userObj(){
				return this.$store.state.userObj;
			}
		},
		methods:{
			allanAbrecht(title){
				if(title == '授权微信个人信息'){
					this.minAccredit();
				}
			},
			userInfo(){
				this.$Api.accountInfo().then(res=>{
					if(res.code == 200){
						uni.showToast({
							title: '授权成功',
						})
						this.$store.commit('updataUserObj', res.data)
					}
				})
			},
			minAccredit(){
				let that = this;
				uni.getUserProfile({
						desc: '获取你的昵称、头像、地区及性别',
						success: res => {
							console.log('获取你的昵称、头像、地区及性别==>',res.userInfo);
							console.log(1);
							let req = {};
							req.phone = '';
							req.address = '';
							req.avatar = res.userInfo.avatarUrl;
							req.gender = 0;
							req.nickname = res.userInfo.nickName;
							req.birthday = '';
							that.$Api.accountEdit(req).then(editRes=>{
								if(editRes.code == 200){
									that.userInfo();
								}
							})
				
						},
						fail: res => {
							console.log(2);
							console.log(res)
							//拒绝授权
							uni.showToast({
								title: '授权失败',
								icon: 'error',
								duration: 2000
							});
							return;
						}
					});
				
			
			
			}
		}
	}
</script>

<style>
</style>
